﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace GShare1._0
{
    //Will Handle the applications
    //Files
    //Folders
    //Path Creation
    //File Checking
    //Updating of file list
    //File list management - events
    public struct FileDownloadInfo
    {
        public string filePathName;
        public string fileExtension;
        public long fileSize;

    }
    public class FileManager
    {

        private string path = Application.StartupPath;
        private string downloadsPath;
        private string serverInfoPath;
        private string savedServersPath;
        private string uploadsPath;
        private List<FileDownloadInfo> fileInfoList;

        public FileManager()
        {
            downloadsPath = path + "\\" + "Downloads";
            serverInfoPath = path + "\\" + "Server Info";
            savedServersPath = path + "\\" + "Saved Servers";
            uploadsPath = path + "\\" + "User Uploads";
            fileInfoList = new List<FileDownloadInfo>();
        }

        public string UploadsPath
        {
            get { return uploadsPath; }
            set { uploadsPath = value; }
        }
        public List<FileDownloadInfo> FileInfoList
        {
            get { return fileInfoList; }
            set { fileInfoList = value; }
        }
        public string Path
        {
            get { return path; }
            set { path = value; }
        }
        public string  DownloadsPath
        {
            get { return downloadsPath; }
            set { downloadsPath = value; }
        }
        public string SavedServersPath
        {
            get { return savedServersPath; }
            set { savedServersPath = value; }
        }        
        public string ServerInfoPath
        {
            get { return serverInfoPath; }
            set { serverInfoPath = value; }
        }
        
        
        

        /// <summary>
        /// Using the application startup path checks if folders exist and creates them
        /// updates the FileInfoList
        /// </summary>
        public void CheckContainingFolder()
        {
            UpdateFileInfoList();
            if (!File.Exists(downloadsPath))
            {
                Directory.CreateDirectory(downloadsPath);
            }
            if (!File.Exists(serverInfoPath))
            {
                Directory.CreateDirectory(serverInfoPath);
            }
            if (!File.Exists(savedServersPath))
            {
                Directory.CreateDirectory(savedServersPath);
            }
            if (!File.Exists(uploadsPath))
            {
                Directory.CreateDirectory(uploadsPath);
            }

        }

        public void UpdateFileInfoList()
        {
            fileInfoList.Clear();
            string[] downloadFileNames = Directory.GetFiles(downloadsPath);
            //check files within Downloads
            foreach (string item in downloadFileNames)
            {
                FileDownloadInfo fileDownloadInfo = new FileDownloadInfo();
                //get the files info
                FileInfo fileInfo = new FileInfo(item);

                fileDownloadInfo.fileSize = fileInfo.Length;
                fileDownloadInfo.filePathName = item;
                fileDownloadInfo.fileExtension = fileInfo.Extension;

                fileInfoList.Add(fileDownloadInfo);
            }


            //Debugging only
            foreach (FileDownloadInfo item in fileInfoList)
            {
                Debug.WriteLine(item.filePathName);
            }
            
        }
    }
}
